package com.syn.synold4s.activity.setting;

import androidx.activity.result.ActivityResult;

import com.gaoyi.basemodule.baseUtil.DensityUtil;
import com.gaoyi.basemodule.baseUtil.LanguageUtils;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseUtil.WindowUtil;
import com.gaoyi.basemodule.baseact.BaseViewActivity;
import com.gaoyi.basemodule.basebean.Event;
import com.gaoyi.basemodule.baseconstant.EventCode;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.syn.synold4s.activity.base.BaseAlertViewActivity;
import com.syn.synold4s.bean.dto.ShortDto;
import com.syn.synold4s.control.ShortPopupWindow;
import com.syn.synold4s.databinding.ActivityLanguageBinding;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class LanguageActivity extends BaseAlertViewActivity<ActivityLanguageBinding> {

    private ShortPopupWindow mItemPopupWindow;
    private List<ShortDto> mData;
    private ShortDto mSel;

    @Override
    protected void initData() {
        WindowUtil.setWindowDecorView(this);
        mBinding.tvLanguage.setText(LanguageUtils.getInstance().getStrLanguage());
        mBinding.tvLanguage.setOnClickListener((view) -> showItemPopupWindow());
        mBinding.settingToolbar.setOnBackListener(this::finish);
        mBinding.btnSave.setOnClickListener((view -> {
            int languageId = Integer.parseInt(mSel.getId());
            if (LanguageUtils.getInstance().getLanguageInt() == languageId) {
                return;
            }
            Locale locale = languageId == 1 ? Locale.TAIWAN : Locale.ENGLISH;
            SPUtil.getInstance().putInt(SPName.LANGUAGE, languageId);
            LanguageUtils.getInstance().change(locale);
            EventBus.getDefault().post(new Event(EventCode.EVENT_LANGUAGE));
        }));
    }

    private void showItemPopupWindow() {
        if (mItemPopupWindow == null) {
            mData = new ArrayList<>();
            boolean english = LanguageUtils.getInstance().isEnglish();
            mData.add(new ShortDto("2", LanguageUtils.getInstance().getEnglishString(), english));
            mData.add(new ShortDto("1", LanguageUtils.getInstance().getChineseString(), !english));
            mItemPopupWindow = new ShortPopupWindow(mContext);
            mItemPopupWindow.setHeight(DensityUtil.dp2px(130));
            mItemPopupWindow.setAnchorView(mBinding.tvLanguage);
            mItemPopupWindow.setOnItemClickListener((adapterView, view, i, l) -> {
                for (int j = 0; j < mData.size(); j++) {
                    mData.get(j).setSelect(false);
                }
                mSel = mData.get(i);
                mData.get(i).setSelect(true);
                mBinding.tvLanguage.setText(mSel.getName());
                mItemPopupWindow.dismiss();
                mBinding.tvLanguage.setSelected(false);
            });
            mItemPopupWindow.setData(mData);
        }
        if (mItemPopupWindow.isShowing()) {
            mItemPopupWindow.dismiss();
            mBinding.tvLanguage.setSelected(false);
        } else {
            mBinding.tvLanguage.setSelected(true);
            mItemPopupWindow.show();
        }
    }

    @Override
    protected ActivityLanguageBinding getViewBinding() {
        return ActivityLanguageBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void event(Event event) {

    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }
}